//
//(C) Copyright 2011 HongKong Airline.,Ltd. 
//
/**
 * orderforminfoServiceImpl
 * 项目名称:代购系统
 *
 * @author Mengxiangliang
 *
 * 功能描述:
 * 
 * 历史版本:2013-01-04 v1.0.0 Mengxiangliang  创建:
 * 
 */
 
package com.gamutsoft.amb.orderinfo.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import com.gamutsoft.amb.commodity.model.Commodity;
import com.gamutsoft.amb.commodity.service.ICommodityService;
import com.gamutsoft.amb.common.bean.QueryExample;
import com.gamutsoft.amb.common.constants.Constants;
import com.gamutsoft.amb.common.exception.BusinessException;
import com.gamutsoft.amb.common.service.impl.GenericServiceImpl;
import com.gamutsoft.amb.common.util.UUIDUtil;
import com.gamutsoft.amb.common.util.VDateTime;
import com.gamutsoft.amb.orderinfo.bean.CommitOrderBean;
import com.gamutsoft.amb.orderinfo.bean.OrderforminfoQueryBean;
import com.gamutsoft.amb.orderinfo.dao.IOrderforminfoDao;
import com.gamutsoft.amb.orderinfo.model.Orderforminfo;
import com.gamutsoft.amb.orderinfo.service.IOrderforminfoService;
import com.gamutsoft.amb.user.model.SystemUser;

public class OrderforminfoServiceImpl extends GenericServiceImpl<Orderforminfo, Long> implements IOrderforminfoService {
	public IOrderforminfoDao orderforminfoDao;
	
	private ICommodityService commodityService;
    public OrderforminfoServiceImpl(IOrderforminfoDao orderforminfoDao) {
		super(orderforminfoDao);
		this.orderforminfoDao = orderforminfoDao;
	}
	
	public ICommodityService getCommodityService() {
		return commodityService;
	}

	public void setCommodityService(ICommodityService commodityService) {
		this.commodityService = commodityService;
	}

	/**
	 * 根据条件查询记录
	 * @author Mengxiangliang
	 * @param  QueryExample
	 * @return OrderforminfoList
	 * @throws BusinessException
	 */
	public List<Map> searchOrderforminfoList(QueryExample qe) throws BusinessException{
	  
	   return orderforminfoDao.searchOrderforminfoList(qe);
	   
	}
	
	/**
	 * 根据ID查询记录
	 * @author Mengxiangliang
	 * @param Long id
	 * @return domain
	 * @throws BusinessException
	 */
	public Orderforminfo searchOrderforminfoById(Long id) throws BusinessException{
	  
	   return orderforminfoDao.searchOrderforminfoById(id);
	   
	}

	/**
	 *  删除记录
	 * @author Mengxiangliang	 
	 * @param id
	 * @throws BusinessException
	 */
	public void deleteOrderforminfoById(Long id) throws BusinessException{
	 
	  orderforminfoDao.deleteOrderforminfoById(id);
	  
	}
	
	/**
	 * 修改记录
	 * @author Mengxiangliang
	 * @param domain
	 * @throws BusinessException
	 */
	public void updateOrderforminfo(Orderforminfo domain) throws BusinessException{
	
	   orderforminfoDao.save(domain);
	   
	}

	/**
	 * 新增记录
	 * @author Mengxiangliang
	 * @param domain
	 * @return
	 * @throws BusinessException
	 */
	public void createOrderforminfo(Orderforminfo domain) throws BusinessException{
	
	   orderforminfoDao.save(domain);
	}

	@Override
	public void createOrderforminfo(List<Commodity> list,SystemUser user)
			throws BusinessException {
		//创建空订单
		Orderforminfo order =new Orderforminfo();
		order.setCode(UUIDUtil.createUUID());
		order.setCreatetime(VDateTime.getCurrentStandardTime());
		order.setStatus(Constants.ORDERINFO_NOPAY);//待支付
		order.setUserId(user.getId());
		Orderforminfo myOrder=orderforminfoDao.save(order);
		//获取商品信息
		BigDecimal sumAmount=new BigDecimal("0.00");
		if(list!=null && list.size()!=0){
			for(Commodity obj: list){
				if(obj!=null && obj.getId()!=null && !"".equals(obj.getId())){
					Commodity domain=commodityService.searchCommodityById(obj.getId());
					if(domain!=null){
						BigDecimal price=domain.getPrice();
						BigDecimal count=new BigDecimal(domain.getBuycount());
						BigDecimal feight=domain.getFreight();
						BigDecimal goodAmount=price.multiply(count);
						goodAmount=goodAmount.add(feight);
						sumAmount.add(goodAmount);
						//对商品进行状态更新操作
						domain.setOrderforminfoid(myOrder.getId());//关联订单
						domain.setStatus(Constants.COMMODITY_ORDER_NOBUY);//未处理
						commodityService.updateCommodity(domain);
					}
				}
			}
			//设置总额
			myOrder.setSummoney(sumAmount);
			//保存
			orderforminfoDao.save(myOrder);
		}
	}

	@Override
	public List<Map> searchOrderforminfoByWhere(QueryExample qe,
			OrderforminfoQueryBean bean) throws BusinessException {
		// TODO Auto-generated method stub
		return orderforminfoDao.searchOrderforminfoByWhere(qe, bean);
	}

	@Override
	public List<Map> searchOrderforminfoByStatus(QueryExample qe, String status,Long userId)
			throws BusinessException {
		// TODO Auto-generated method stub
		return orderforminfoDao.searchOrderforminfoByStatus(qe, status,userId);
	}

	@Override
	public List<Map> searchOrderforminfoByWhere(QueryExample qe,
			CommitOrderBean bean) throws BusinessException {
		// TODO Auto-generated method stub
		return orderforminfoDao.searchOrderforminfoByWhere(qe, bean);
	}

}